<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserRequest;
use App\User;
use DB;
use Hash;
use App\boke;
use Markdown;
use App\citys;
use Cart;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login()
    {
        return view('login');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function relog()
    {
        return view('relog');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function dorelog(UserRequest $request)
    {
        $user = new User;
        $user -> username = $request -> username;
        $user -> password = Hash::make($request -> password);
        $user -> email = $request -> email;
        //图片上传
        if($request->hasFile('img')){
            $filename = time().rand(10000,90000);
            $suffix = $request->file('img')->getClientOriginalExtension(); 
            $dirname = './uploads/';
            $file = $filename.'.'.$suffix;
            $request -> file('img') -> move($dirname,$file);
            $user -> img = $dirname.$file;
        }
        if($user->save()){
            return redirect('/login');
        }else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function dologin(Request $request)
    {
        $username = $request->username;

        //读取数据库
        $user = User::where('username',$username)->first();

        //判断
        if(empty($user)) {
            return back();
        }

        //检测密码
        if(Hash::check($request->password, $user->password)) {
            //写入session
            session(['id' => $user->id]);

            return redirect('/shou');
        }else{
            return back();
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function shou()
    {
        return view('shou');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function logout(Request $request)
    {
        $request->session()->flush();
        return redirect('/login');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function tupian()
    {
        $imgs = User::paginate(3);
        return view('tupian',['img'=>$imgs]);
    }
    /**
     * 博客
     */
      public function boke()
    {
        //获取数据
        $boke = boke::paginate(3);
        return view('demo.index',['boke'=>$boke]);
    }
    /**
     * 添加博客页面
     */
      public function add()
    {
        return view('demo.add');
    }
    /**
     * 执行添加
     */
      public function doadd(Request $request)
    {
       $boke = new boke;
       $boke -> title = $request -> title;
       // $boke->content = Markdown::convertToHtml($request->content);
       $boke->content = $request->content;
       if($boke -> save()){
            return redirect('/boke');
       }else{
            return back(); 
       }
    }
    /**
     * 阅读页面
     */
     public function yuedu($id)
    {
        $boke = boke::find($id);
        $boke->content = Markdown::convertToHtml($boke->content);
        return view('demo.yuedu',['boke'=>$boke]);
    }
    /**
     * 修改页面
     */
     public function xiugai($id)
    {
        $boke = boke::find($id);
        return view('demo.xiugai',['boke'=>$boke]);
    }
    /**
     * 执行修改
     */
     public function doxiugai(Request $request,$id)
    {
        $boke = boke::find($id);
        $boke -> title = $request -> title;
        $boke -> content = $request -> content;
        if($boke -> save()){
            return redirect('/boke');
       }else{
            return back(); 
       }

    }
    /**
     * 删除
     */
     public function del($id)
    {
        $boke = boke::where('id',$id)->delete();
        return redirect('/boke');
    }
    /**
     * 预览
     */
     public function yulan(Request $request)
    {
       return Markdown::convertToHtml($request->content);
    }
    /**
     * 无限遍历
     */
      public function wuxian($arr, $id=0,$lv=0)
    { 
       static $tree = [];
       foreach ($arr as $v){
            if($v['pid']==$id){
                $v['lv'] = $lv;
                $tree[] = $v;
                $this->wuxian($arr, $v['id'],$lv+1);
            }
       }
       return $tree;  
    }
    /**
     * 遍历
     */
     public function bianli()
    {
       $citys = citys::get();
       $citys = $this -> wuxian($citys);
       return view('admins.admin-add',['citys'=>$citys]);
    }
    /**
     * 执行添加数据库
     */
    public function dobianli(Request $request)
    {
       $citys = new citys;
       $citys -> title = $request -> title;
       $citys -> pid = $request -> adminRole;
       $citys->save();
       return redirect('/bianli');
    }
    /**
     * 商品列表
     */
    public function gouwu(Request $request)
    {
       $products = [
        1 => ['id'=>1,'name'=>'夏季短裙','price'=>11,'des'=>'夏季短裙'],
        2 => ['id'=>2,'name'=>'职业套装','price'=>22,'des'=>'职业套装'],
        3 => ['id'=>3,'name'=>'青春休闲','price'=>33,'des'=>'青春休闲'],
       ];
       return view('product',['products'=>$products]);
    }
    /**
     * 加入购物车
     */
     public function addcart($id)
    {
       $products = [
        1 => ['id'=>1,'name'=>'夏季短裙','price'=>11,'des'=>'夏季短裙'],
        2 => ['id'=>2,'name'=>'职业套装','price'=>22,'des'=>'职业套装'],
        3 => ['id'=>3,'name'=>'青春休闲','price'=>33,'des'=>'青春休闲'],
       ];
       $product = $products[$id];
       Cart::add($product['id'],$product['name'],1,$product['price']);
       return redirect('/cart');
    }
    /**
     * 购物车
     */
     public function che(Request $request)
    {
       $carts = Cart::content();
       $count = Cart::count();
       $total = Cart::subtotal();
       return view('cart',['carts'=>$carts,'count'=>$count,'total'=>$total]);
    }
    /**
     * 移除购物车
     */
    public function remove($id)
    {
       Cart::remove($id);
       return redirect('/cart');
    }
    /**
     * 清空
     */
    public function qingkong($id)
    {
       Cart::destroy();
       return redirect('/cart');
    }
}
